package dao

import (
	"context"
	"errors"
	"mini-app/internal/dal"
	"mini-app/internal/models"
	"strings"
	"time"

	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type PublishDeviceGroup struct{}

func (PublishDeviceGroup) Create(ctx context.Context, db *gorm.DB, name string) (err error) {
	m := dal.Use(db).PublishDeviceGroup
	var d models.PublishDeviceGroup
	d.ID, _ = helper.UUID{}.String()
	d.Name = name
	d.CreatedAt = time.Now().Unix()
	m.WithContext(ctx).Create(&d)
	return nil
}

// 列表
func (PublishDeviceGroup) List(ctx context.Context, db *gorm.DB, ID string) (res []*models.PublishDeviceGroup, err error) {
	m := dal.Use(db).PublishDeviceGroup
	tx := m.WithContext(ctx)
	if ID != "" {
		if strings.Contains(ID, ",") {
			tx = tx.Where(m.ID.Eq(ID))
		} else {
			tx = tx.Where(m.ID.In(strings.Split(ID, ",")...))
		}
	}
	res, err = tx.Where(m.DeletedAt.Eq(0)).Find()
	return
}

// 设备分组编辑
func (PublishDeviceGroup) Update(ctx context.Context, db *gorm.DB, ID, name string) (err error) {
	m := dal.Use(db).PublishDeviceGroup
	if ID == "" {
		return errors.New("ID不能为空")
	}
	m.WithContext(ctx).
		Where(m.ID.Eq(ID)).
		Update(m.Name, name)
	return
}

// 设备分组删除
func (PublishDeviceGroup) Delete(ctx context.Context, db *gorm.DB, ID string) (err error) {
	m := dal.Use(db).PublishDeviceGroup
	if ID == "" {
		return errors.New("ID不能为空")
	}
	m.WithContext(ctx).
		Where(m.ID.Eq(ID)).
		Update(m.DeletedAt, time.Now().Unix())

	// 删除关联的设备
	pd := dal.Use(db).PublishDevice
	pd.WithContext(ctx).
		Where(pd.GroupID.Eq(ID)).
		Update(pd.DeletedAt, time.Now().Unix())
	return
}

// 同名是否存在
func (pdg PublishDeviceGroup) SameNameExist(ctx context.Context, db *gorm.DB, name, ExceptID string) (count int64) {
	m := dal.Use(db).PublishDeviceGroup
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if name != "" {
		tx = tx.Where(m.Name.Eq(strings.TrimSpace(name)))
	}
	if ExceptID != "" {
		tx = tx.Where(m.ID.Neq(ExceptID))
	}
	count, _ = tx.Count()
	return
}

// 投放设备组是否存在
func (pdg PublishDeviceGroup) DeviceGroupExist(ctx context.Context, db *gorm.DB, ID string) (count int64) {
	m := dal.Use(db).PublishDeviceGroup
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if ID != "" {
		tx = tx.Where(m.ID.Eq(ID))
	}
	count, _ = tx.Count()
	return
}

// 设备分组列表
func (pdg PublishDeviceGroup) PublishDeviceGroupList(ctx context.Context, db *gorm.DB, name string, page, limit int) (data []*models.PublishDeviceGroup, count int64, err error) {
	m := dal.Use(db).PublishDeviceGroup
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))

	if strings.TrimSpace(name) != "" {
		tx = tx.Where(m.Name.Like("%" + strings.TrimSpace(name) + "%"))
	}
	list, count, _ := tx.Order(m.ID.Desc()).FindByPage(Offset(page, limit))
	return list, count, nil
}

// 根据分组名称获取分组ID
func (PublishDeviceGroup) GetGroupIDByName(ctx context.Context, db *gorm.DB, groupName string) (groupID string, err error) {
	m := dal.Use(db).PublishDeviceGroup
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0)).
		Where(m.Name.Eq(groupName))
	err = tx.Pluck(m.ID, &groupID)
	return
}
// 根据分组ID获取分组名称
func (PublishDeviceGroup) GetGroupNameByID(ctx context.Context, db *gorm.DB, groupID string) (groupName string, err error) {
	m := dal.Use(db).PublishDeviceGroup
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0)).
		Where(m.ID.Eq(groupID))
	err = tx.Pluck(m.Name, &groupName)
	return
}
